package com.content.Service.Impl.artist;

import com.common.Utils.JwtUtil;
import com.content.Entity.artist.School;
import com.content.Entity.artist.SchoolFeatures;
import com.content.Entity.artist.SchoolInheritance;
import com.content.Mapper.artist.SchoolMapper;
import com.content.Service.artist.SchoolService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Service
public class SchoolServiceImpl implements SchoolService {
    @Autowired
    private SchoolMapper schoolMapper;

    @Override
    public PageInfo<School> getSchoolList(Integer pageNum, Integer pageSize, String schoolName) {
        PageHelper.startPage(pageNum,pageSize);
        List<School> schoolList = schoolMapper.getSchoolList(schoolName);
        return new PageInfo<>(schoolList);
    }

    @Override
    public void addSchool(School school) {
        String token = school.getToken();
        String username = JwtUtil.extractUsername(token);
        school.setCreateUser(username);
        school.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        school.setSchoolId(makeSchoolId());
        schoolMapper.addSchool(school);



        for (SchoolFeatures feature : school.getFeatures()) {
            // 将每个特点保存到数据库
            SchoolFeatures schoolFeatures = new SchoolFeatures();
            schoolFeatures.setSchoolId(school.getSchoolId());
            schoolFeatures.setFeaturesId(makeFeaturesId());
            schoolFeatures.setTitle(feature.getTitle());
            schoolFeatures.setDescription(feature.getDescription());
            schoolMapper.addSchoolFeatures(schoolFeatures);
        }


    }



    @Override
    public void updateSchool(School school) {
        String token = school.getToken();
        String username = JwtUtil.extractUsername(token);
        school.setUpdateUser(username);
        school.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        schoolMapper.updateSchool(school);

        if (school.getFeatures() != null) {
            schoolMapper.deleteSchoolFeatures(school.getSchoolId());
            for (SchoolFeatures feature : school.getFeatures()) {
                // 将每个特点保存到数据库
                SchoolFeatures schoolFeatures = new SchoolFeatures();
                schoolFeatures.setSchoolId(school.getSchoolId());
                schoolFeatures.setFeaturesId(makeFeaturesId());
                schoolFeatures.setTitle(feature.getTitle());
                schoolFeatures.setDescription(feature.getDescription());
                schoolMapper.addSchoolFeatures(schoolFeatures);
            }
        }else{
            schoolMapper.deleteSchoolFeatures(school.getSchoolId());
        }



    }

    @Override
    public void deleteSchool(String schoolId) {

        schoolMapper.deleteSchool(schoolId);
        schoolMapper.deleteSchoolFeatures(schoolId);

    }

    @Override
    public List<SchoolFeatures> getSchoolFeatures(String schoolId) {
        return schoolMapper.getSchoolFeatures(schoolId);
    }

    @Override
    public List<SchoolInheritance> getSchoolInheritance(String schoolId) {

        return schoolMapper.getSchoolInheritance(schoolId);
    }

    public String makeSchoolId(){
        return "S" + System.currentTimeMillis();
    }
    public String makeFeaturesId(){
        return "SF" + System.currentTimeMillis();
    }
}
